#!/bin/sh

# 当前脚本必须要在 deploy 文件夹下执行，要不然路径会有错误

server_name="st-demo" # 只需要改动名称即可

# ---------------------------------- 下方内容无需改动 ------------------------------------
harbor_repo="st-server"
harbor_user="admin"
harbor_password="vdqXhy4nzKbjBK6Z"
product_id=1
env=5
devops_version_addr="http://devops.stwin.net:31800/v1/version/incrOnlyCode"

# 从地址获取 version 信息
curl "${devops_version_addr}?product_id=${product_id}&env=${env}&server_name=${server_name}" |  jq -r '.data.version_string' > ./version.txt
# 确认已经删除历史的二进制文件
rm -f ${server_name}
version=$(cat ./version.txt)

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -tags jsoniter -o ${server_name}-nopack ../main.go || exit

echo ========${server_name}-1.打包完成========

upx -9 ${server_name}-nopack -o ${server_name} || exit

echo ========${server_name}-2.压缩完成========

docker build -t ${server_name} . || exit

echo ========${server_name}-3.镜像打包完成========

rm -f ${server_name}
rm -f ${server_name}-nopack
rm -f ./version.txt

echo ========${server_name}-4.清理打包临时文件========

docker login -u ${harbor_user} -p ${harbor_password} harbor.stwin.net

echo ========${server_name}-5.harbor鉴权通过========

docker tag ${server_name} harbor.stwin.net/${harbor_repo}/${server_name}:latest
docker tag ${server_name} harbor.stwin.net/${harbor_repo}/${server_name}:$version
docker push harbor.stwin.net/${harbor_repo}/${server_name}:latest
docker push harbor.stwin.net/${harbor_repo}/${server_name}:$version

docker rmi harbor.stwin.net/${harbor_repo}/${server_name}:latest
docker rmi harbor.stwin.net/${harbor_repo}/${server_name}:$version
docker rmi ${server_name}

echo ========${server_name}-6.镜像推送完成========
